// pages/shortMsg/components/orderList/orderList.js
import * as API from '../../../../api/msgApi';
import * as util from '../../../../utils/util';

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    id: {
      type: String
    },
    reachTime:{
      type: String,
      value:'',
      observer: function (reachTime) {
        let oldTime = this.data.reachTimes;
        if (reachTime && reachTime != oldTime){      
          this.setData({
            reachTimes: reachTime
          })
          if (this.data.hasMore) {
            this.setData({
              pageNo: ++this.data.pageNo
            });
            this.loadData(false);
          }
        }
      }
    },
    pageNo: {
      type: Number,
      value: 1,
      observer: function (newVal, oldVal) {
        console.log("newVal", newVal)
        if (newVal) {
          // this.setData({
          //   'ec': getChart(newVal, this)
          // });
          // if (this.data.ready) {
          //   this.init();
          // }
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    id: '',
    dataSource: [],
    pageNo: 1,
    pageSize: 10,
    total: 0,
    hasMore: false,
    hasRequest: false,
    networkError: false,
    total: 0,
    reachTimes:''
  },
  /**
   * 组件的方法列表
   */
  methods: {
    clear: function () {
      // 清空数据重新加载
      this.setData({
        dataSource: [],
        pageNo: 1,
        pageSize: 10,
        hasMore: false
      });
    },
    loadData: function (options) {
      API.getOrderList(this.data.id, this.data.pageNo, this.data.pageSize).then(data => {
        let arr = data.orderList || [];
        if (!options) {
          arr = this.data.dataSource.concat(data.orderList || []);
        }
        let hasMore = arr.length < data.totalCount;
        this.setData({
          dataSource: arr,
          hasMore: hasMore,
          hasRequest: true,
          networkError: false,
          total: data.totalCount
        });
      }, err => {
        this.setData({
          networkError: true
        });
      })
    },
    gotoDetail: function (e) {
      util.preventMoreClick(this, () => {
        const orderId = e.currentTarget.dataset.orderId;
        if (orderId) {
          wx.navigateTo({
            url: '/pages/order/orderDetail/orderDetail?id=' + orderId
          });
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */

  ready: function () {
    this.setData({
      id: this.id,
      pageNo: this.properties.pageNo
    });
    this.clear();
    this.loadData(true);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 重新加载
    this.clear();
    this.loadData(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 增量加载
    // if (this.data.hasMore) {
    //   this.setData({
    //     pageNo: ++this.data.pageNo
    //   });
    //   this.loadData(false);
    // }
  }
})